Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Encapsulation in Java

  • Encapsulation is defined as the collection of data into a single entity. It is the mechanism that connects the data the code manipulates and the code itself. Encapsulation can also be viewed as a barrier that prevents code from the outside of the barrier from accessing the data.
  • Encapsulation technically refers to the fact that a class's variables or data are kept secret from all other classes and are only available through member functions of the class in which they are specified.


Table Of Content

  • Encapsulation in Java
  • Advantages of Encapsulation
  • Read Only class in Java
  • Write Only class in Java
  • Encapsulation example






  • As with encapsulation, the data in a class is protected from other classes using the data hiding concept, which is accomplished by making the members or methods of a class private. The class is then made available to the user or the public without revealing any information about its internal workings.
  • The data in a class is hidden from other classes, similar to encapsulation, using the data hiding concept, which is accomplished by making the members or methods of a class private, and the class is exposed to the user or the public without disclosing any implementation-specific information using the abstraction concept, so it is also referred to as a combination of data-hiding and abstraction.
  • Encapsulation is achieved by writing public methods in the class to set and retrieve the values of variables and by making all of the class's variables private.
  • With the setter and getter methods, encapsulation is better defined.

Advantages of encapsulation

  • Data Hiding: Data Hiding is a method of limiting our data members' access by concealing the implementation specifics. Data hiding is also possible with encapsulation. The user won't be aware of how the class is internally implemented. The user won't be able to see how the class is keeping variables' values in order. The user will only be aware that variables are initialized with the given value and that we are passing the values to a setter method.
  • Flexibility Depending on our needs, we can make the class's variables read-only or write-only. If we want to make the variables read-only, we must remove the setter methods from the program, such as setEmpName() and setEmpId(), or if we want to make the variables write-only, we must remove the get methods, such as getEmpName() and getEmpId().
  • Reusability: Additionally, encapsulation makes it easier to reuse code and adapt to changing needs.
  • Easy to taste : For unit testing, encapsulated code is simple to test.





Example of Encapsulation in Java

 // Java Encapsulation  example  
class Encapsulation{
	 
    private String  Name; // Private is using to achive Encapsulation
    
    public String getName() { // getter method
    	return Name; 
    }
    
    public void setName(String  Name){ // setter method
        this.Name = Name;
    } 
}
 
class Main {
    public static void main(String[] args){
    	Encapsulation encapsulation = new Encapsulation();
 
    	encapsulation.setName("Mohan");
 
        System.out.println("The Name of the person is : " +encapsulation.getName());
    }
}



Output:

Mohan 


Read Only class in Java


Java Read Only class
class Encapsulation{
	 
    private String  Name="Alok"; // Private is using to achive Encapsulation
    
    public String getName() { // getter method
    	return Name; 
    }
}

Now, you can't change the value of the Name data member which is "Alok"


Encapsulation encapsulation = new Encapsulation();

encapsulation.setName("Mohan");    //C.T Error





Write Only class in Java


Java Write Only class
class Encapsulation{
	 
    private String  Name; // Private is using to achive Encapsulation
        
    public void setName(String  Name){ // setter method
        this.Name = Name;
    } 
}

Now, you can't get the value of the Name data member you can only change


Encapsulation encapsulation = new Encapsulation();

 System.out.println(encapsulation.getName());    //C.T Error


Example of Encapsulation in Java

class Encapsulation{	 
    private String  EmpName; 
    private int  EmpId;
	public String getEmpName() {
		return EmpName;
	}
	public void setEmpName(String empName) {
		EmpName = empName;
	}
	public int getEmpId() {
		return EmpId;
	}
	public void setEmpId(int i) {
		EmpId = i;
	}   
}
 
class Main {
    public static void main(String[] args){
    	Encapsulation encapsulation = new Encapsulation();
    	encapsulation.setEmpName("Mohan");
    	encapsulation.setEmpId(421658);
        System.out.println("The Name of the Emp is : " +encapsulation.getEmpName()+" 
                                                                        & EmpID is : "+encapsulation.getEmpId());
    }
}



Output:

The Name of the Emp is : Mohan & EmpID is : 421658 

Java Package Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Encapsulation
Java Package
Java Access Modifiers
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.